/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.zhtj.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eit.eitsite.common.web.BaseController;
import com.zhuoyue.zhtj.entity.Memo;
import com.zhuoyue.zhtj.service.MemoService;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 备忘录Controller
 * @author hss
 * @version 2017-09-02
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/memo")
public class MemoController extends BaseController {
	@Autowired
	private MemoService memoService;

	/**
	 * 新增备忘录
	 * @param memo
	 * @param response
     * @return
     */
	@RequestMapping(value = "savememo")
	public String saveMemo(Memo memo,HttpServletResponse response,HttpServletRequest request) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if(memoService.saveMemo(memo)){
			List<Memo> list=memoService.findList(memo);
			if(list!=null){
				return this.renderString(response,ServerResponse.createBySuccess("新增成功",list));
			}
		}
		return this.renderString(response, ServerResponse.createByErrorMessage("新增失败"));
	}

	/**
	 * 更新备忘录
	 * @param memo
	 * @param response
     * @return
     */
	@RequestMapping(value = "updatememo")
	public String updateMemo(Memo memo,HttpServletResponse response) {
		memoService.update(memo);
		List<Memo> list=memoService.findList(memo);
		if(list!=null){
			return this.renderString(response,ServerResponse.createBySuccess("更新成功",list));
		}
		return this.renderString(response, ServerResponse.createByErrorMessage("更新失败"));
	}

	/**
	 * 获取备忘录
	 * @param memo
	 * @param response
     * @return
     */
	@RequestMapping(value = "getmemo")
	public String getMemo(Memo memo,HttpServletResponse response){
		List<Memo> list=memoService.findList(memo);
		if(list!=null) {
			return super.renderString(response, ServerResponse.createBySuccess("获取备忘录成功", list));
		}
		return this.renderString(response,ServerResponse.createByErrorMessage("您尚无任何备忘录"));
	}

	/**
	 * 删除备忘录
	 * @param memo
	 * @param response
     * @return
     */
	@RequestMapping(value = "deletememo")
	public String delete(Memo memo,HttpServletResponse response) {
		if(memoService.deleteMemo(memo)) {
			return this.renderString(response, ServerResponse.createBySuccessMessage("删除成功"));
		}
		return this.renderString(response,ServerResponse.createByErrorMessage("删除失败"));
	}

}